package com.example.mydianping.controller;

import com.example.mydianping.dto.Result;
import com.example.mydianping.dto.UserDTO;
import com.example.mydianping.pojo.Blog;
import com.example.mydianping.service.BlogService;
import com.example.mydianping.utils.UserHolder;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    //关注推送
    @GetMapping("/of/follow")
    public Result getFeed(@RequestParam("max") Long max,@RequestParam(value="offset",defaultValue="0") Integer offset ){
        return blogService.getFeed(max,offset);
    }

    //查看博主的历史博文
    @GetMapping("/of/user/{id}")
    public Result getBlogs(@PathVariable("id") Long id){
        return blogService.getBlogs(id);
    }

    //保存探店博文到数据库
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog){
        if(blog == null){
            return Result.fail("博文内容不能为空");
        }
        UserDTO user = UserHolder.getUser();
        blog.setUserId(user.getId());
        return blogService.saveBlag(blog);
    }

    @GetMapping("/{id}")
    public Result getBlog(@PathVariable("id") Long id){
        Blog blog = blogService.queryBlog(id);
        return Result.ok(blog);
    }

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id){
        return blogService.updateBlog(id);
    }

    //查看最早点赞的前五名用户
    @GetMapping("/likes/{id}")
    public Result likes(@PathVariable("id") Long id){
        return blogService.likes(id);
    }

}
